﻿@using System.Reflection
@{
    var F = Html.F();
}

@functions {

    /// <summary>
    /// 排除不推荐的主题
    /// </summary>
    /// <param name="themeStr"></param>
    /// <returns></returns>
    private string OnlyRecommendedTheme(string themeStr)
    {
        var excluedThemes = new List<string> {
                "Metro_Blue", "Metro_Dark_Blue", "Metro_Gray", "Metro_Green", "Metro_Orange",
                "Pure_Red",
                "Black_Tie", "Blitzer", "Dot_Luv", "Eggplant", "Excite_Bike", "Humanity", "Le_Frog", "Mint_Choc", "Overcast", "Pepper_Grinder",
                "Redmond", "Smoothness", "Sunny", "Swanky_Purse", "Trontastic", "UI_Darkness", "UI_Lightness", "Vader", "Hot_Sneaks",
                "bootstrap_pure", "custom_light_green",
                "image_black_sky", "image_green_drip",  "image_green_poppy", "image_blue_star", "image_blue_drip", "image_blue_moon", "image_red_dawn", "image_orange_light" };
        if (excluedThemes.Contains(themeStr))
        {
            return "Pure_Black";
        }
        else
        {
            return themeStr;
        }
    }

    private bool IsSystemTheme(string themeName)
    {
        themeName = themeName.ToLower();
        string[] themes = Enum.GetNames(typeof(Theme));
        foreach (string theme in themes)
        {
            if (theme.ToLower() == themeName)
            {
                return true;
            }
        }
        return false;
    }

}

@{
    var pm = F.PageManager;

    // 主题
    var themeCookie = Context.Request.Cookies["Theme"];
    if (!String.IsNullOrEmpty(themeCookie))
    {
        themeCookie = OnlyRecommendedTheme(themeCookie);

        // 是否为内置主题
        if (IsSystemTheme(themeCookie))
        {
            // 先清空CustomTheme，再设置Theme
            pm.CustomTheme(String.Empty);
            pm.Theme((Theme)Enum.Parse(typeof(Theme), themeCookie, true));
        }
        else
        {
            pm.CustomTheme(themeCookie);
        }
    }

    // 语言
    var langCookie = Context.Request.Cookies["Language"];
    if (!String.IsNullOrEmpty(langCookie))
    {
        Language lang;
        if (Enum.TryParse<Language>(langCookie, true, out lang))
        {
            pm.Language(lang);
        }
        else
        {
            pm.CustomLanguage(langCookie);
        }
    }


    var loadingCookie = Context.Request.Cookies["Loading"];
    if (!String.IsNullOrEmpty(loadingCookie))
    {
        int loadingNumber = Convert.ToInt32(loadingCookie);
        pm.LoadingImageNumber(loadingNumber);
        // 使用GIF加载动画时，需要先禁用CSS加载动画
        pm.LoadingCSSNumber(0);
    }

    // 优先使用CSS加载动画
    var loadingCSSCookie = Context.Request.Cookies["Loading_CSS"];
    if (!String.IsNullOrEmpty(loadingCSSCookie))
    {
        int loadingCSSNumber = Convert.ToInt32(loadingCSSCookie);
        pm.LoadingCSSNumber(loadingCSSNumber);
    }


    // 1. 仅显示基础版示例， 2. 基础版
    // 上述两种情况都要禁用EnableAnimation、DisplayMode、MobileAdaption
    bool showOnlyBase = false;
    var showOnlyBaseCookie = Context.Request.Cookies["ShowOnlyBase"];
    if (!String.IsNullOrEmpty(showOnlyBaseCookie))
    {
        showOnlyBase = Convert.ToBoolean(showOnlyBaseCookie);
    }

    if (showOnlyBase || Constants.IS_BASE)
    {
        pm.MobileAdaption(false);
        pm.EnableAnimation(false);
    }

    // 显示模式
    var modeCookie = Context.Request.Cookies["DisplayMode"];
    if (!String.IsNullOrEmpty(modeCookie))
    {
        try
        {
            pm.DisplayMode((DisplayMode)Enum.Parse(typeof(DisplayMode), modeCookie, true));
        }
        catch (Exception)
        {
            pm.DisplayMode(DisplayMode.Normal);
        }
    }



    // // 是否移动页面
    // var page = ViewContext.RouteData.Values["page"];
    // var pageName = (page == null ? "" : page.ToString().ToLower());
    // if (pageName.StartsWith("/mobile/"))
    // {
    //     // 强制设为移动设备自适应
    //     pm.MobileAdaption(true);
    // }


}

<!DOCTYPE html>
<html>
<head>
    <title>@Html.Raw(ViewBag.Title)</title>
    @if (ViewBag.SourceFiles != null)
    {
        <meta name="sourcefiles" content="@ViewBag.SourceFiles" />
    }

    @F.RenderCss()

    <link rel="stylesheet" type="text/css" href="~/res/css/common.css?v@(GlobalConfig.ProductVersion)" />

    @RenderSection("head", false)
</head>
<body>
    @Html.AntiForgeryToken()

    @F.PageManager

    @RenderSection("body", true)

    @F.RenderScript()

    <script type="text/javascript" src="~/res/js/common.js?v@(GlobalConfig.ProductVersion)"></script>

    @RenderSection("script", false)

</body>
</html>
